AddSaleOrderButton = Ext.extend(Ext.Button, {
	setMainWindow : function(main_win) {
		this.main_win = main_win;
	},
	saleOrderDeal : function() {
		this.sendDataAndGetMes({
			'saleOrderId' : this.main_win.sale_id_hidden.getValue()
		});
	},
	saleBatchInsert : function() {
		if( !this.main_win.validateSaleData()) {
			return;
		}
		
		var recs = this.main_win.sale_item_store.getRange();
		var datas = getRecordPropValue(recs);
		
		this.main_win.sale_status_hidden.setValue(this.id == 'saleOrder_save' + this.main_win.versionCode ? 
													'0' : '3');

		this.main_win.sale_wkflId_hidden.setValue('3');
		
//		this.main_win.sale_buyer_id_hidden.setValue(this.main_win.partner_combox.getValue());
		
		this.main_win.sale_partnerPyCodeBySeller_hidden.setValue(this.main_win.partner_combox.lookupValue);
													
		var formValues = this.main_win.getFormValues();
				
		this.sendDataAndGetMes({
			'saleOrder' : formValues,
			'saleDetails' : Ext.util.JSON.encode(datas)
		});
	},
	saleBatchUpdate : function() {
		if( !this.main_win.validateSaleData()) {
			return;
		}
		
		this.main_win.sale_status_hidden.setValue(this.id == 'saleOrder_update' + this.main_win.versionCode ? 
													'0' : '3');
		
//		this.main_win.sale_buyer_id_hidden.setValue(this.main_win.partner_combox.getValue());
		
		this.main_win.sale_partnerPyCodeBySeller_hidden.setValue(this.main_win.partner_combox.lookupValue);
													
		var formValues = this.main_win.getFormValues();

		var obj = this.main_win.getModifiedData();
		
		this.sendDataAndGetMes({
			'saleOrder' : formValues,
			'all' : Ext.util.JSON.encode(obj.all),
			'add' : Ext.util.JSON.encode(obj.add),
			'update' : Ext.util.JSON.encode(obj.update)
		});
		
	},
	saleOrderClose : function() {
		this.main_win.close();
	},
	saleNextOne : function() {
		var desktop = Uec3.App.getDesktop(); 
		desktop.launchWindowByParam("addOutSaleOrderWindowCreator",{});
//		alert('');
	},
	saleRestore : function() {
		this.sendDataAndGetMes({
			saleOrderId : this.main_win.sale_id_hidden.getValue()
		});
	},
	saleReset : function() {
		this.main_win.sale_panel.getForm().reset();
		this.main_win.sale_item_store.removeAll();
		this.main_win.partner_combox.clearValue();
		this.main_win.address_combox.clearValue();
//		this.main_win.address_combox.triggerTags[0].params = null;
//		this.main_win.address_combox.triggerTags[1].params = null;
		this.sendDataAndGetMes();
	},
	saleDelete : function() {
		this.sendDataAndGetMes({
			'saleOrderId' : this.main_win.sale_id_hidden.getValue()
		});
	},
	
	saleOrderTakeOver : function() {
		this.sendDataAndGetMes({
			'saleOrderId' : this.main_win.sale_id_hidden.getValue()
		});
	},
	saleOrderTakeOverUpdateOrSubmit : function() {
		this.main_win.sale_status_hidden.setValue(this.id == 'saleOrder_takeOverUpdate' + this.main_win.versionCode ? '2' : '3');
		
		var formValues = this.main_win.getFormValues();

		var obj = this.main_win.getModifiedData();
		
		this.sendDataAndGetMes({
			'saleOrder' : formValues,
			'all' : Ext.util.JSON.encode(obj.all),
			'add' : Ext.util.JSON.encode(obj.add),
			'update' : Ext.util.JSON.encode(obj.update)
		});
	},
	saleQuery : function() {
		var desktop = Uec3.App.getDesktop(); 
		desktop.launchWindowByParam("showSaleOrderWindowCreator",{});
	},
	saleOnlineQuery : function() {
		this.saleQuery();
	},
	saleNotOnlineQuery : function() {
		this.saleQuery();
	},
	sendDataAndGetMes : function(params, notLoadedMask) {
		if (!notLoadedMask) {
			this.main_win.loadMask = new Ext.LoadMask(this.main_win.getId(), {
						removeMask : true,
						msg : '数据传送中......'
					});
			// this.loadMask.enable();
			this.main_win.loadMask.show();
		}
		Ext.Ajax.request({
					url : this.url,
					params : params,
					success : function(data) {
						var vo = Ext.decode(data.responseText);
						this.vo = vo;
						
						this.main_win.status = this.vo.status;
						
						if (!this.vo.loadedMesesKeys)
							return;
						var load_funcs_keys = this.vo.loadedMesesKeys
								.split(':');
						this.vo.load_num = load_funcs_keys.length;
						if (this.vo.load_num == 1
								&& load_funcs_keys[0] == 'callbackAlertFunc') {
							this.vo.load_num = 0;
						}
						Ext.each(load_funcs_keys, function(func_key_str) {
							
							eval('this.' + func_key_str + '()');
						}.createDelegate(this));
					}.createDelegate(this)
				});
	},
	loadButtonsToolBar : function(params) {
		
		this.main_win.removeOldSaleButtons();
		Ext.Ajax.request({
			url : this.loadBtnsUrl,
			params : params ? params : {
				'status' : this.vo.status,
				'saleOrderId' : this.vo.saleOrderId
			},
			success : function(data) {
				var bo = Ext.decode(data.responseText);

				if (bo.disa == '1') {
					
					var items = this.main_win.sale_panel.getForm().items;
					
					for(var i=0; i<items.getCount(); i++) {
						var item = items.get(i);
						if(item.xtype != 'grid') {
							item.disable();
						}
					}
								
					if(this.main_win.saleOrder_toolbar) {
						this.main_win.saleOrder_toolbar.removeAll();
						if(this.main_win.status == '3') {
							this.main_win.saleOrder_toolbar.add({text : '[生成出库单]', iconCls : 'icon-edit', handler : this.main_win.createSales.createDelegate(this.main_win)});
							this.main_win.saleOrder_toolbar.doLayout();
						}
					} 
					
					this.main_win.orderQuantityEditor.disable();
						
//					if(this.main_win.creator.ordersNameEn == 'online_sale_order' ) {
//					} else {
//					}
					
					if( this.main_win.status == '-1' || this.main_win.status == '0' || this.main_win.status == '2') {
						this.main_win.quantityEditor.enable();
					} else {
						this.main_win.quantityEditor.disable();
					}
					
				} else {
					
					var items = this.main_win.sale_panel.getForm().items;
					
					for(var i=0; i<items.getCount(); i++) {
						var item = items.get(i);
						if(item.xtype != 'grid') {
							item.enable();
						}
					}
					
					if(this.main_win.creator.ordersNameEn == 'online_sale_order' ) {
						this.main_win.orderQuantityEditor.disable();
					} else {
						if(this.main_win.status != '-1' && this.main_win.status != '0') {
							this.main_win.orderQuantityEditor.disable();
						}
					}
					
					if( this.main_win.status == '-1' || this.main_win.status == '0' || this.main_win.status == '2') {
						this.main_win.quantityEditor.enable();
					} else {
						this.main_win.quantityEditor.disable();
					}
				}

				this.main_win.createNewSaleButtons( bo.btns );
				this.loadBtns = true;
				this.callbackAlertFunc.createDelegate(this)();
			}.createDelegate(this)
		});
	},
	loadSaleForm : function(params) {
		this.main_win.sale_panel.getForm().load({
					url : this.loadSaleOrderUrl,
					params : params ? params : {
						'saleOrderId' : this.vo.saleOrderId
					},
					success : function(form, action) {
						var record = action.result;
						var address = '地址:' + (record.data.province ? record.data.province : '') + (record.data.city ? record.data.city : '') + 
	  	   	             	(record.data.district ? record.data.district : '') + (record.data.street ? record.data.street : '') + 
	  	   	             	(record.data.receiverName ? ";联系人:" + record.data.receiverName : '') + 
	  	   	             	(record.data.phone ? ";电话:" + record.data.phone : '') + 
	  	   	             	(record.data.mobile ? ";手机:" + record.data.mobile : '');
	  	   	             	
						if(record.data.workFlowId == '3') {
							this.main_win.partner_combox.initializeValue({
								'name' : record.data.partnerNameBySeller,
								'pyCode' : record.data.pyCode,
								'id' : record.data.partnerIdBySeller
							});
								
							this.main_win.address_combox.initializeValue({
								'areaId' : record.data.areaId,
								'address' : address
							});
							
//							this.main_win.address_combox.triggerTags[0].params = {'partnerId' : record.data.partnerIdBySeller};
//							this.main_win.address_combox.triggerTags[1].params = {'partnerId' : record.data.partnerIdBySeller};
							
						} else {
							this.main_win.sale_panel.getForm().findField('address').setValue(
								address
							);
						}
						
						this.loadForm = true;
						this.callbackAlertFunc.createDelegate(this)();
					}.createDelegate(this)
				});
	},
	loadDetailsGrid : function(params) {
		this.main_win.sale_item_store.load({
					params : params ? params : {
						'saleOrderId' : this.vo.saleOrderId
					},
					callback : function() {
						this.loadGrid = true;
//						alert(this.main_win.status);
//						this.main_win.captureSaleStoreEvent.createDelegate(this.main_win)();
						this.callbackAlertFunc.createDelegate(this)();
					}.createDelegate(this)
				});
	},
	callbackAlertFunc : function() {
		
//		alert(this.vo.open);
		/*if(this.vo.open && this.vo.open == '0') {
			this.main_win.close();
			if(this.vo.sel == '0') {
				Ext.Msg.alert('错误提示', this.vo.sale_alert_mes);
				return;
			}
			Ext.Msg.confirm("提示", this.vo.sale_alert_mes, function(btnId) {
				if(btnId == 'no') {
					return;
				}
				
				Ext.Ajax.request({
					url : '',
					params : {
						'ownerId' : this.vo.ownerId,
						'companyId' : this.vo.companyId
					},
					success : function(data) {
						var obj = Ext.decode(data.responseText);
						Ext.Msg.alert('提示', obj.mess);
					}
				});
				
			}.createDelegate(this));
			return;
		}*/
		
		if (this.vo.load_num == 1) {
			if( !this.loadBtns) {
				return;
			}
		}
		
		if(this.vo.load_num == 3) {
			if ( !this.loadForm || !this.loadGrid || !this.loadBtns) {
				return;
			}
		}
		
		if (this.main_win.loadMask) {
			this.main_win.loadMask.hide();
		}
//		alert(this.vo.sale_alert_mes);
		if(this.vo.sale_alert_mes) {
			var alerts = this.alertMeses[this.vo.sale_alert_mes].split(':');
			if( this.vo.remove == '1' ) {
				Ext.Msg.alert(alerts[0], alerts[1], function() {
					this.close();
				}.createDelegate(this.main_win));
			} else {
				this.main_win.setSellerCode.createDelegate(this.main_win,
							[ this.main_win.sale_panel.getForm().findField('codeOfSeller').getValue() ])();
				if(this.main_win.status + '' == '3') {
					Ext.Msg.confirm("提示", alerts[1] + ',您是否现在就生成销售出库单?', function(btnId) {
						if(btnId == 'no') {
							return;
						} else {
							this.main_win.createSales();
						}
					}.createDelegate(this));
				} else {
					Ext.Msg.alert(alerts[0], alerts[1]);
				}
			}
		}
		
		/*if(this.main_win.noticeId) {
			Ext.Ajax.request({
				url : RES_PATH + '/notice/remove.do',
				params : {'noticeIds' : this.main_win.noticeId},
				success : function(res) {
					
					var vo = Ext.decode(res.responseText);
					if(vo.success ) {
//						alert('');
						this.main_win.noticeId = null;
						
//						var noticeWin = this.main_win.creator.app.getDesktop.getWindow('notice');
//						if(noticeWin) {
//							noticeWin.creator.noticeGrid.getStore().reload();
//						}
					}
					
				}.createDelegate(this)
			});
		}*/
	}
});